﻿using System.Collections.Generic;
using Newtonsoft.Json;
[JsonObject(MemberSerialization.OptIn)]
public class RoomManager:Dict<string,Room>
{
    /// <summary>
    /// 添加一个房间到我们的房间管理器
    /// </summary>
    /// <param name="room">欲被添加的房间</param>
    public void Add(Room room)
    {
        Add(room.Id, room);
    }
    public override void Remove(string key)
    {
        if (IsExist(key))
        {
            GetValue(key).DestroySelf();
            Dictionary.Remove(key);
        }
    }
    /// <summary>
    /// 从我们的房间管理器中移除一个房间
    /// </summary>
    /// <param name="room">欲被移除的房间实例</param>
    public void Remove(Room room)
    {
        Remove(room.Id);
    }
    [JsonProperty]
    /// <summary>
    /// 获取我们房间管理器中的所有房间
    /// </summary>
    public Room[] Rooms
    {
        get
        {           
            return new List<Room>(Dictionary.Values).ToArray();
        }
    }
    /// <summary>
    /// 在所有房间列表中查找一个成员,并且移除
    /// </summary>
    /// <param name="clientId"></param>
    public void RemoveMember(string clientId)
    {
        foreach (Room room in Rooms)
        {
            if (room.GetMember(clientId) != null)
            {
                room.GetMember(clientId).Room = null;
                room.Remove(clientId);
            }
        }
    }
/// <summary>
    /// 重写ToString方法,返回房间管理器的JSON对象
    /// </summary>
    /// <returns></returns>
    public override string ToString()
    {
        return JsonConvert.SerializeObject(this);
    }
}